﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace MadPropsDemo.RecipeEditor
{
    static class Mapper
    {
        public static Model.RecipeDTO ToDTO(Recipe recipe)
        {
            var dto = new Model.RecipeDTO();

            dto.Name = recipe.Name.Value;
            dto.Description = recipe.Description.Value;

            dto.Ingredients = recipe.Ingredients.Select(i =>
                new Model.IngredientDTO()
                {
                    Name = i.Name.Value,
                    Quantity = i.Quantity.Value,
                    UnitOfMeasure = i.UnitOfMeasure.Value,
                }).ToList();

            return dto;
        }

        public static Recipe FromDTO(Model.RecipeDTO dto)
        {
            var recipe = new Recipe();

            recipe.Name.Value = dto.Name;
            recipe.Description.Value = dto.Description;

            var ingredients = dto.Ingredients.Select(i =>
            {
                var ingredient = new Ingredient();
                ingredient.Name.Value = i.Name;
                ingredient.Quantity.Value = i.Quantity;
                ingredient.UnitOfMeasure.Value = i.UnitOfMeasure;
                return ingredient;
            });
            recipe.Ingredients = new ObservableCollection<Ingredient>(ingredients);

            return recipe;
        }
    }
}
